const express = require('express');
const connection = require('../mysql') // 获取连接实例
const app = express();
const $sql = require('../db');

app.post('/getallrecords',(req,res) => {
	let pageSize = req.body.pageSize // 页大小
	let pageCurrent = req.body.pageCurrent // 当前页
	let start=(pageCurrent-1)*pageSize; // 起始位置
	console.log(start,pageSize)
	connection.query(`select * from record LIMIT ${start},${pageSize}`,(err, result) => {
		if (err) {
			console.log(err)
			return res.send(err);
		} else {
			return res.send(result);
		}
	})
})

app.get('/getallrecord',(req,res) => {
	var sql = $sql.record_sql.getalldata;
	connection.query(sql,(err, result) => {
		if (err) {
			console.log(err)
			return res.send(err);
		} else {
			return res.send(result);
		}
	})
})
//根据类型获取记录信息
app.post('/gettypedata',(req,res) => {
	var sql = $sql.record_sql.gettypedata;
	var params = req.body.type;
	connection.query(sql,params,(err,result) => {
		if (err) {
			console.log(err)
			return res.send(err);
		} else {
			return res.send(result);
		}
	})
})
//根据类型和account获取记录信息
app.post('/databyacty',(req,res) => {
	var sql = $sql.record_sql.databyacty;
	var params = [req.body.type,req.body.account];
	connection.query(sql,params,(err,result) => {
		if (err) {
			console.log(err)
			return res.send(err);
		} else {
			return res.send(result);
		}
	})
})
//根据id获取记录信息
app.post('/getdata',(req,res) => {
	var sql = $sql.record_sql.getdata;
	var params = req.body.id;
	connection.query(sql,params,(err,result) => {
		if (err) {
			console.log(err)
			return res.send(err);
		} else {
			return res.send(result);
		}
	})
})
//根据account获取记录信息
app.post('/getdatabyid',(req,res) => {
	var sql = $sql.record_sql.getdatabyid;
	var params = req.body.account;
	connection.query(sql,params,(err,result) => {
		if (err) {
			console.log(err)
			return res.send(err);
		} else {
			return res.send(result);
		}
	})
})
//根据id删除记录信息
app.delete('/delrecdata',(req,res) => {
	var sql = $sql.record_sql.delgetdata;
	var params = req.body.id;
	connection.query(sql,params,(err,result) => {
		if (err) {
			console.log(err)
			return res.send(err);
		} else {
			return res.send(result);
		}
	})
})
//更新记录
app.put('/uprecorddata', (req, res) => {
	var sql = $sql.record_sql.uprecorddata;
    var params = [req.body.data,req.body.type,req.body.note,req.body.stoname,req.body.stospe,
				  req.body.stoid,req.body.num,req.body.purchase_price,req.body.amount,req.body.supid,req.body.supname,req.body.bank_account,
				  req.body.wname,req.body.whentryname,req.body.title,req.body.id,]
	connection.query(sql,params,(err, result) => {
		if (err) {
			console.log(err)
			return res.send(err);
		} else {
			return res.send(result);
		}
	})
})
//添加记录
app.put('/stockadd', (req, res) => {
	var sql = $sql.record_sql.recadd;
    var params = [req.body.data,req.body.type,req.body.note,req.body.stoname,req.body.stospe,
				  req.body.stoid,req.body.num,req.body.purchase_price,req.body.amount,req.body.supid,
				  req.body.supname,req.body.bank_account,req.body.wname,req.body.nowwar,req.body.whentryname,req.body.title]
	connection.query(sql,params,(err, result) => {
		if (err) {
			console.log(err)
			return res.send(err);
		} else {
			return res.send(result);
		}
	})
})
//根据时间获取记录信息
app.post('/gettimedata',(req,res) => {
	var sql = $sql.record_sql.gettimedata;
	var params = req.body.data;
	connection.query(sql,params,(err,result) => {
		if (err) {
			console.log(err)
			return res.send(err);
		} else {
			return res.send(result);
		}
	})
})

module.exports = app;